/*
 *************************************************************************
 *西瑞克斯通信技术股份有限公司研发管理系统
 *版 本: 1.0   
 *日 期：02/05/2012    
 *作 者： 王浩 QQ:873987060   
 *************************************************************************   
 */
package com.application.yfproject.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.application.yfproject.entity.YfProject;
import com.application.yfproject.struts.form.YfProjectForm;
import com.manager.commons.CommonUtil;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;

/**
 * Description:YfProjectDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: Code Generator <br />
 * 
 * History: <br />
 * 
 * 02/05/2012 王浩.
 * 
 */
public class YfProjectDAO extends HibernateGenericDao<YfProject> {
	private static final Logger logger = Logger.getLogger(YfProjectDAO.class);

	private YfProject yfProject;

	public YfProject getYfProject() {
		return this.yfProject;
	}

	public void setYfProject(YfProject yfProject) {
		this.yfProject = yfProject;
	}

	public Integer getAllCountYfProject(YfProjectForm yfProjectForm) {		
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(0) FROM yf_project t left join usertable u on t.project_manager = u.id left join department b on t.project_org = b.id WHERE 1=1 ");
		sql = getStringBuffer(yfProjectForm, sql, args);
		return super.getCount(sql.toString(), args.toArray());
	}
    
	public  String getMaxProjectNo(String pix ){
		String temp = "000";
		StringBuffer buf = new StringBuffer();
		
		buf.append("select max(project_no) maxNo from yf_project t  where  project_no like '%" + pix + "%' ");
		Map projectNoMap = getJdbcTemplate().queryForMap(buf.toString());
		String maxNo = (String) projectNoMap.get("maxNo");
		System.out.println("maxNo=" + maxNo);
        if(StringUtils.isBlank(maxNo)){
        	return pix + "001";
        }
        maxNo = maxNo.substring(5);
        int no = Integer.parseInt(maxNo) + 1;
        maxNo = String.valueOf(no);
        maxNo = temp.substring(maxNo.length()) + maxNo;
		return pix + maxNo;
	}
	
	
	public void deleteYfProjects(YfProjectForm yfProjectForm) {
		String hql = " delete from YfProject where id in  ( '" + yfProjectForm.getIds() + "')";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteYfProjects failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchYfProject(YfProjectForm yfProjectForm,
			final PageBean pageBean) {
		StringBuffer sql = new StringBuffer();
		List args = new ArrayList();
		sql.append(" SELECT t.*,u.rname rname,b.name project_org_name FROM yf_project t left join usertable u  on t.project_manager = u.id left join department b on t.project_org = b.id WHERE 1=1");
		sql = getStringBuffer(yfProjectForm, sql, args);
		sql.append(" order by t.create_date desc");
		return getPageInfo(pageBean, sql.toString(), args);
	}

	public boolean isProjectManager(String userid){
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		List args = new ArrayList();
		sql.append(" SELECT project_manager FROM yf_project WHERE project_manager = ?");
		args.add(userid);
		List list = getPageInfo(null, sql.toString(), args);
		if(list!=null && list.size()>0){
			flag = true;
		}
		return flag;
	}
	public Integer getAllCountYfProjectXMST(YfProjectForm yfProjectForm) {		
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(0) FROM yf_project t left join usertable u on t.project_manager = u.id left join department b on t.project_org = b.id WHERE 1=1 ");
		sql = getStringBufferXMST(yfProjectForm, sql, args);
		return super.getCount(sql.toString(), args.toArray());
	}
	
	public List searchYfProjectXMST(YfProjectForm yfProjectForm,
			final PageBean pageBean) {
		StringBuffer sql = new StringBuffer();
		List args = new ArrayList();
		sql.append(" SELECT t.*,u.rname rname,b.name project_org_name FROM yf_project t left join usertable u  on t.project_manager = u.id left join department b on t.project_org = b.id WHERE 1=1");
		sql = getStringBufferXMST(yfProjectForm, sql, args);
		sql.append(" order by t.create_date desc");
		return getPageInfo(pageBean, sql.toString(), args);
	}
	
	/**
	 * 查询条件的封装
	 * 
	 * @param departmentForm
	 * @return
	 */
	private StringBuffer getStringBuffer(YfProjectForm yfProjectForm,
			StringBuffer buf,List args) {
		/*
		 * 需要加入查询条件时封装
		 */

		if (StringUtils.isNotBlank(yfProjectForm.getProjectName())) {
			buf.append(" and t.project_name like ? ");
			args.add("%" + yfProjectForm.getProjectName().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfProjectForm.getProjectType())) {
			buf.append(" and t.project_type =? ");
			args.add(yfProjectForm.getProjectType().trim());
		}
		if (StringUtils.isNotBlank(yfProjectForm.getProjectOrg())) {
			buf.append(" and t.project_org =?");
			args.add(yfProjectForm.getProjectOrg().trim());
		}
		if (StringUtils.isNotBlank(yfProjectForm.getProjectNo())) {
			buf.append(" and t.project_no=? ");
			args.add(yfProjectForm.getProjectNo().trim());
		}
		if (null != yfProjectForm.getInitiateYear() && yfProjectForm.getInitiateYear().longValue() != 0) {
			buf.append(" and t.initiate_year = ? ");
			args.add(yfProjectForm.getInitiateYear());
		}
		if (StringUtils.isNotBlank(yfProjectForm.getProjectManager())) {
			buf.append(" and t.project_manager=? ");
			args.add(yfProjectForm.getProjectManager().trim());
		}
		if (StringUtils.isNotBlank(yfProjectForm.getId())) {
			buf.append(" and t.id = ? ");
			args.add(yfProjectForm.getId().trim());
		}
		if(StringUtils.isNotBlank(yfProjectForm.getOrg())){
			buf.append(" and t.project_org = ?");
//			if("2012021916152137014".equals(yfProjectForm.getOrg())){
//				yfProjectForm.setOrg("1");
//			}
//			if("2012022022490435526".equals(yfProjectForm.getOrg())){
//				yfProjectForm.setOrg("2");
//			}
//			if("2012022022060595430".equals(yfProjectForm.getOrg())){
//				yfProjectForm.setOrg("3");
//			}
			args.add(yfProjectForm.getOrg());
		}
		if("2".equals(yfProjectForm.getQueryType())){
			buf.append(" and t.project_no in (select project_no from yf_project_plan)");
		}
		
		
		return buf;
	}
	
	
	/**
	 * 查询条件的封装
	 * 
	 * @param departmentForm
	 * @return
	 */
	private StringBuffer getStringBufferXMST(YfProjectForm yfProjectForm,
			StringBuffer buf,List args) {
		/*
		 * 需要加入查询条件时封装
		 */

		if (StringUtils.isNotBlank(yfProjectForm.getProjectName())) {
			buf.append(" and t.project_name like ? ");
			args.add("%" + yfProjectForm.getProjectName().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfProjectForm.getProjectType())) {
			buf.append(" and t.project_type =? ");
			args.add(yfProjectForm.getProjectType().trim());
		}
		if (StringUtils.isNotBlank(yfProjectForm.getProjectOrg())) {
			buf.append(" and t.project_org =?");
			args.add(yfProjectForm.getProjectOrg().trim());
		}
		if (StringUtils.isNotBlank(yfProjectForm.getProjectNo())) {
			buf.append(" and t.project_no=? ");
			args.add(yfProjectForm.getProjectNo().trim());
		}
		if (null != yfProjectForm.getInitiateYear() && yfProjectForm.getInitiateYear().longValue() != 0) {
			buf.append(" and t.initiate_year = ? ");
			args.add(yfProjectForm.getInitiateYear());
		}
		if (StringUtils.isNotBlank(yfProjectForm.getProjectManager())) {
			buf.append(" and t.project_manager=? ");
			args.add(yfProjectForm.getProjectManager().trim());
		}
		if (StringUtils.isNotBlank(yfProjectForm.getId())) {
			buf.append(" and t.id = ? ");
			args.add(yfProjectForm.getId().trim());
		}
		if(StringUtils.isNotBlank(yfProjectForm.getOrg())){
			buf.append(" and t.project_org = ?");
			args.add(yfProjectForm.getOrg());
		}
		buf = CommonUtil.filterDataXMST(buf,args);
		
		return buf;
	}
}
